/**
 * 图表设计器
 *
 * create by 姚巨磊  2017年3月14日15:20:30
 *
 * 左侧为表格项，显示概要信息
 * 右侧为设计区域，用于拖拽放布局。
 *
 *
 */
define(["require", "jquery", 'public/app'], function (require, $, app) {

    function designer($container) {
        this._$container = $container;
        this._initLayout();
        this._init();
    }

    designer.prototype = {
        _initLayout: function () {
            $('#muuri_designer_layout').layout();
        },
        /**
         * 初始化url地址
         * @private
         */
        _initUrlPath: function () {
            this.urlPaths = {
                getConfig: app.urlBuild("/ge/dev/stu/page"),//查询配置信息
                saveConfig:app.urlBuild("/ge/dev/stu/save"),
                removeStu:app.urlBuild("/ge/dev/stu")
            };
        },

        /**
         * 初始化类
         * @private
         */
        _init: function () {
            this._initUrlPath();

            var self = this;
            self._initComponents();
            //通过DataReady先准备好所有页面需要的数据，然后再对页面控件进行统一的初始化
            var dataReady = app.getDataReady();
            dataReady.pushDataSource({//逐个放入所有数据源
                url: this.urlPaths.getUnit,
                key: 'units'
            });
            dataReady.once(function (readyData) {//所有数据源都加载后执行一次方法
                self._loadComboboxData(readyData);
            });
        },

        /**
         * 初始化控件
         * @param readyData
         * @private
         */
        _initComponents: function () {
            this._initDatagrid();
            this._initDialog();
            this._initCombobox();
            this._initEventBinding();
        },
        _initCombobox:function(){
            $('#stu_manager_queryUnit').combobox({
                valueField:'id',
                textField:'name',
            });
            $('#stu_manager_edit_Unit').combobox({
                valueField:'id',
                textField:'name',
            });
        },
        _loadComboboxData:function(readyData){
            $('#stu_manager_queryUnit').combobox('loadData',readyData.units.data);
            $('#stu_manager_edit_Unit').combobox('loadData',readyData.units.data);
        },
        _initDatagrid:function(){
            this._$container.find("#stu_manager_datagrid").datagrid({
                //url:this.urlPaths.getStu,
                pagination : true,//分页控件
                fit: true,   //自适应大小
                method:'GET',
                border:false,
                striped:true,
                singleSelect:true,
                fitColumns:true,
                toolbar:"#stu_manager_datagrid_tb",
                columns:[[
                    {field : 'unNo',title:'机顶盒序列号',width : '100%'}
                ]]
            });
        },
        _initDialog:function(){
            var self= this;
            this._$container.find("#stu_manager_datagrid_dlg").dialog({
                title:'企业管理',
                modal: true,
                width:400,
                height:300,
                closed:true,
                buttons:[{
                    text:'保存',
                    handler:function(){
                        self.actions.saveStu(self);
                    }
                },{
                    text:'退出',
                    handler:function(){
                        $("#stu_manager_datagrid_dlg").dialog("close");
                    }
                }]

            });
        },
        /**
         * 事件绑定
         * @private
         */
        _initEventBinding: function () {
            var self = this;
            $("#stu_manager_imageUrl").textbox({onChange:function(){
            }});
            $("#stu_manager_queryBtn").bind('click', function(){
                self.actions.queryStu(self);
            });
            $('#stu_manager_add').bind('click', function(){
                self.actions.addStu();
            });
            $('#stu_manager_edit').bind('click', function(){
                self.actions.editStu();
            });
            $('#stu_manager_remove').bind('click', function(){
                self.actions.deleteStu(self);
            });
            $('#stu_manager_edit_Unit').combobox({onSelect:function(){
                self.actions.synCombobox();
            }});


        },

        /**
         * 按钮事件
         */
        actions: {
            queryStu:function(self){
                var unno = $("#stu_manager_queryUnNo").textbox("getValue");
                var unit = $('#stu_manager_queryUnit').combobox('getValue');
                var queryParams = $('#stu_manager_datagrid').datagrid('options').queryParams;

                if(unno != null && unno != ""){
                    queryParams.unNo = unno;
                }
                if(unit != null && unit != ""){
                    queryParams.unitId = unit;
                }
                $('#stu_manager_datagrid').datagrid('options').url = self.urlPaths.getStu;
                $('#stu_manager_datagrid').datagrid('options').queryParams = queryParams;
                $('#stu_manager_datagrid').datagrid('reload');
            },
            saveStu:function(self){
                var formValue = $("#stu_manager_form").serializeJson();
                if(formValue.id == "" || formValue.id == null){
                    delete formValue.id;
                }
                $.ajax({
                    url:self.urlPaths.saveStu,
                    type:"POST",
                    cache:false,
                    contentType: "application/json",
                    async : false,
                    dataType: "json",
                    data: JSON.stringify(formValue) ,
                    success: function(){
                        $('#stu_manager_datagrid').datagrid('reload');
                        $("#stu_manager_datagrid_dlg").dialog("close");
                    },

                });

            },
            addStu:function(){
                $("#stu_manager_form").form("clear");
                $("#stu_manager_datagrid_dlg").dialog("open");
            },
            editStu:function(){
                $("#stu_manager_form").form("clear");
                var selectRow = $('#stu_manager_datagrid').datagrid("getSelected");
                if(selectRow != null){
                    $("#stu_manager_form").form('load',selectRow);
                    if(selectRow.showText == 1){
                        $('#stu_manager_showText').switchbutton("check");
                    }
                    $("#stu_manager_datagrid_dlg").dialog("open");
                }else{
                    $.alertErrorMessage("请选择需要编辑的机顶盒");
                }

            },
            deleteStu:function(self){
                var selectRow = $('#stu_manager_datagrid').datagrid("getSelected");
                if(selectRow != null){
                    $.messager.confirm('确认','您确认想要删除记录吗？',function(r){
                        if (r){
                            $.ajax({
                                url:self.urlPaths.removeStu+"/"+selectRow.id,
                                type:"DELETE",
                                cache:false,
                                contentType: "application/json",
                                async : false,
                                success: function(){
                                    $('#stu_manager_datagrid').datagrid('reload');
                                },
                            });


                        }
                    });
                }else{
                    $.alertErrorMessage("请选择需要删除的机顶盒");
                }
            },
            synCombobox:function(){
                var editValue = $('#stu_manager_edit_Unit').combobox('getValue');
                $('#stu_manager_queryUnit').combobox('select',editValue);
            }
        }

    };

    return designer;
});